
//index.js
var config = require("../config.js")
var PollingSocket = require("../../utils/socket.js")
// var disabledToken = require("../disabledToken.js")

// use gaode map service
var gaodeConfig = require("../../utils/gaodeConfig.js")
var amapFile = require('../../utils/amap-wx.js');
var broadcast = require("../../utils/broadcast.js")
// var Scan = require("../../utils/util.js")

const app = getApp()
Page({
  data: {
    disabled: false,
    
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    
  },
  onLoad: function (options) {
    let that = this
    //that.resetMapControls()
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else {
      // 监听登录状态改变事件
      broadcast.on("login-success", function (data, event) {
        //判断是否绑定手机号
        
          //获取用户信息
          var rememberMe = wx.getStorageSync('token')
          if (rememberMe != null && rememberMe != "") {
            wx.request({
              url: config.url_all + '/api/getUserinfo',
              header: {
                'Rememberme': rememberMe,
                'content-type': 'application/x-www-form-urlencoded'
              },
              method: 'POST',
              success: function (res) {
                var type = res.data.type
                var phone = res.data.data.phone
                var roleName = res.data.data.roleList[0].roleName//用户角色
                
                console.log(res.data.data.phone)//手机号
                console.log(res.data.data.roleList[0].remark)//用户角色
                //判断是否绑定手机号
                if(phone!==""){
                  //判断是否是运营商
                  if (roleName == "operator") {
                    //运营商跳转到信息补充页
                    wx.navigateTo({
                      url: '../information/information',
                    })
                  } else if (roleName == "user") {
                    wx.showModal({
                      title: '提示',
                      content: '您不是我们的运营商，想成为运营商可以联系我们哦~',
                      showCancel: false
                    })
                  }
                }else{
                  wx.navigateTo({
                    url: '../login/login',
                  })
                }
               
              }
            })
          }
        that.setData({
          userInfo: app.globalData.userInfo,
          hasUserInfo: false,
          disabled: false
        })
      })
      broadcast.on("login-error", function (data, event) {
        that.setData({
          disabled: false
        })
      })
    }
    // 调用wx.getLocation系统API,获取并设置当前位置经纬度
    wx.getLocation({
      type: "gcj02", // 坐标系类型
      // 获取经纬度成功回调
      success: (res) => { // es6 箭头函数，可以解绑当前作用域的this指向，使得下面的this可以绑定到Page对象
        console.log(res)
        this.setData({  // 为data对象里定义的经纬度默认值设置成获取到的真实经纬度，这样就可以在地图上显示我们的真实位置
          longitude: res.longitude,
          latitude: res.latitude,
          myLatitude: res.latitude,
          myLongitude: res.longitude
        })
      }
    });
    
  },
  login() {
    broadcast.fire("login")
  },
  bindGetUserInfo: function (e) {
    this.setData({
      disabled: false
    })
    broadcast.fire("login")
    console.log(e.detail.userInfo)
  },
  onUnload: function () {
    // 页面关闭
  },

  onMoreInfo: function () {
    this.setData({
      showMoreInfo: !this.data.showMoreInfo
    })
  }
})